using System;
using System.Data.SqlClient;
using PointOfSale.Global;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

namespace PointOfSale.Class
{
	public class clsPurchaseDetail
	{
		private int purchaseId;
		private double qty;
		private double rate;
		private int unitId;
		private double amount;
		private int itemId;

		public clsPurchaseDetail()
		{
		}

		#region Properties.
		public int PurchaseId
		{
			get
			{
				return purchaseId;
			}
			set
			{
				purchaseId = value;
			}
		}
		public int ItemId
		{
			get
			{
				return itemId;
			}
			set
			{
				itemId = value;
			}
		}	
		public double Quantity
		{
			get
			{
				return qty;
			}
			set
			{
				qty = value;
			}
		}
		public double Rate
		{
			get
			{
				return rate;
			}
			set
			{
				rate = value;
			}
		}
		public int UnitId 
		{
			get
			{
				return unitId;
			}
			set
			{
				unitId = value;
			}
		}
		public double Amount
		{
			get
			{
				return amount;
			}
			set
			{
				amount = value;
			}
		}
		#endregion

		public void Add()
		{
			String strInsert = "INSERT into tblPurchaseDetail (PurchaseId, ItemId, Qty, Rate, UnitId, Amount) " + 
				"VALUES ('"		+
				purchaseId		+ "', '" +
				itemId			+ "', '" +
				qty				+ "', " +
				rate			+ ", '" +						
				unitId			+ "', " +
				amount			+ ")" ;
			clsDatabase.ExecuteSQL(strInsert, clsGlobal.g_DataConn);
		}

		public static DataTable GetDanhSachMatHang(int pID)
		{
			String strSelect = "SELECT a.itemID, a.qty, a.rate, a.unitID, a.amount, b.itemName, c.unitName "
				+ " from tblPurchaseDetail a inner join tblItem b on a.itemID = b.itemID"
				+ " inner join tblUnit c on a.unitID = c.unitID"
				+ " where a.purchaseID = " + pID
				+ " order by itemname asc";
			DataTable dt = clsDatabase.GetDataToTable(strSelect, clsGlobal.g_DataConn);
			return dt;
		}

		public static void XoaHoaDon(int purchaseId)
		{
			string deleteCommand = "delete tblpurchasedetail where purchaseid = " + purchaseId;
			clsDatabase.ExecuteSQL(deleteCommand, clsGlobal.g_DataConn);
		}
	}
}
